
#ifndef _DIGITAL_LPF_H_
#define _DIGITAL_LPF_H_

#include "stdint.h"
#include "arm_math.h"


typedef struct
{
    uint16_t cnt;
	uint16_t length;

    float32_t  sum;
	float32_t  value;
    float32_t* temp;
	float32_t  rslt;

}STR_SLAVG_f;




typedef struct
{
	float32_t sf;
	float32_t value_last;
	
}STR_1ST_LPF;



typedef struct
{
	float32_t a1;
	float32_t a2;
	float32_t b0;
	float32_t b1;
	float32_t b2;
	
	float32_t y0;
	float32_t y1;
	float32_t y2;
	float32_t x0;
	float32_t x1;
	float32_t x2;

}STR_2ND_LPF;



/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

static inline float32_t LPF_1ST_order(STR_1ST_LPF *hdl, float32_t value)
{
	float32_t rslt;
		
	rslt = (value - hdl->value_last) * hdl->sf + hdl->value_last;
		
	hdl->value_last = rslt;
	
	return rslt;
}



/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

static inline float32_t LPF_2ND_order(STR_2ND_LPF *hdl, float32_t value)
{
	hdl->x0 = value;
	
	hdl->y0 = (hdl->b0 * hdl->x0 + hdl->b1 * hdl->x1 + hdl->b2 * hdl->x2) - hdl->a1 * hdl->y1 - hdl->a2 *hdl->y2;
	 
	hdl->y2 = hdl->y1;
	hdl->y1 = hdl->y0;
	
	hdl->x2 = hdl->x1;
	hdl->x1 = hdl->x0;
	
	return hdl->y0;
}

extern void SLAVG_f_init(STR_SLAVG_f *hdl, float32_t *temp, uint16_t length);
extern float32_t SLAVG_f(STR_SLAVG_f *hdl, float32_t value);
extern void LPF_1ST_order_init(STR_1ST_LPF *hdl, float32_t fs, float32_t fc);
extern void LPF_2ND_order_init(STR_2ND_LPF *hdl, float32_t fs, float32_t fc);

#endif




